# import numpy as np
# import matplotlib.pyplot as plt
# from env import ENVIRONMENT
# env1=ENVIRONMENT()
# # 按照公式计算最优的吞吐量！!!!!!
#
# p = 1 / 3 # TDMA节点个数/总的节点个数
# #对于 z<0：𝑇𝑜𝑝𝑡=𝑝⋅(1−𝑞1)+(1−𝑝)⋅𝑞1⋅(1−𝑞1)
# #z≥0：𝑇𝑜𝑝𝑡=(1−𝑞1)
#
#
# TONT = 0
# n=1
# print(env1.z)
# if env1.z < 0:
#     TONT = p * (1-env1.SAaction) + (1 - p) * env1.SAaction * (1-env1.SAaction)
# else:
#     TONT = 1 - env1.SAaction
#
# # 输出最优网络吞吐量
# print("TONT=",TONT)
#
# # 定义绘图函数my_plot，它接受两个文件路径作为参数
# def my_plot(file1, file2, file3):
#     # 设置最大迭代次数
#     max_iter = 10000
#     # 设置滑动窗口的大小
#     N = 5000
#
#     # 从文本文件中加载智能体奖励和TDMA奖励数据
#     agent_reward = np.loadtxt(file1)
#     tdma_reward = np.loadtxt(file2)
#     sa_reward = np.loadtxt(file3)
#
#
#     # 初始化智能体和TDMA的吞吐量数组，都设置为0
#     throughput_agent = np.zeros((1, max_iter))
#     throughput_tdma = np.zeros((1, max_iter))
#     throughput_sa = np.zeros((1, max_iter))
#     # 创建一个全为1的数组，表示每个时间槽的理想（最优）累积奖励
#     total_optimal = np.ones(max_iter) * TONT
#
#     # 初始化累积奖励的临时变量
#     agent_temp_sum = 0
#     tdma_temp_sum = 0
#     sa_temp_sum = 0
#     # 循环遍历最大迭代次数
#     for i in range(0, max_iter):
#         # 如果当前迭代小于N（滑动窗口大小）
#         if i < N:
#             # 更新累积奖励
#             agent_temp_sum += agent_reward[i]
#             tdma_temp_sum += tdma_reward[i]
#             sa_temp_sum += sa_reward[i]
#             # 计算智能体和TDMA的吞吐量（平均奖励）
#             throughput_agent[0][i] = agent_temp_sum / (i + 1)
#             throughput_tdma[0][i] = tdma_temp_sum / (i + 1)
#             throughput_sa[0][i] = sa_temp_sum / (i + 1)
#         else:
#             # 如果当前迭代大于等于N，使用滑动窗口计算吞吐量
#             # 更新累积奖励，减去窗口第一个奖励并加上新奖励
#             agent_temp_sum += agent_reward[i] - agent_reward[i - N]
#             tdma_temp_sum += tdma_reward[i] - tdma_reward[i - N]
#             sa_temp_sum += sa_reward[i] - sa_reward[i - N]
#             # 计算智能体和TDMA的吞吐量（平均奖励），除以滑动窗口大小N
#             throughput_agent[0][i] = agent_temp_sum / N
#             throughput_tdma[0][i] = tdma_temp_sum / N
#             throughput_sa[0][i] = sa_temp_sum / N
#
#     # 绘制智能体和TDMA总吞吐量的线
#     total_line, = plt.plot(throughput_agent[0] + throughput_tdma[0] + throughput_sa[0], color='#236B8E', lw=1.2, label='total')
#     # 绘制理想（最优）吞吐量的线
#     total_optimal_line, = plt.plot(total_optimal, color='orange', lw=1.8, label='total optimal')
#
#     # 设置x轴和y轴的显示范围
#     plt.xlim((0, max_iter))
#     plt.ylim((-0.05, 1))
#     plt.xlabel('Time Slot')
#     plt.ylabel('Throughput')
#     # 显示网格
#     #plt.grid()
#     # 显示图例
#     plt.legend(handles=[total_line, total_optimal_line], loc='best')
#
# # 循环创建两个图形
# for i in range(1, 2):
#     # 创建新的图形
#     plt.figure(i)
#     # 调用my_plot函数进行绘图
#     my_plot('rewards/agent_len5e4_M40_111.txt',
#             'rewards/TDMA_len5e4_M40_111.txt',
#             'rewards/SA_len5e4_M40_111.txt',
#             )
# # 显示所有图形
# plt.show()

# import numpy as np
# import matplotlib.pyplot as plt
# from TDMA import run_tdma_simulation
# from env import ENVIRONMENT
#
# def calculate_TONT(env):
#     p = 2 / 5  # TDMA节点个数/总的节点个数
#
#     if env.z < 0:
#         TONT = p * env.z_num1 + (1 - p) * env.z_num3
#     else:
#         TONT = env.z_num1
#
#     return TONT
#
# def my_plot(file1, file2, file3, file4, file5):
#     # 设置最大迭代次数
#     max_iter = 5000
#
#     # 从文本文件中加载智能体、TDMA 和 SA 的奖励数据
#     agent_reward = np.loadtxt(file1)
#     tdma_reward = np.loadtxt(file2)
#     sa_reward = np.loadtxt(file3)
#
#     # 加载已经平滑处理过的 SA 和 TDMA 的奖励数据
#     sa_reward_smoothed = np.loadtxt(file4)
#     tdma_reward_smoothed = np.loadtxt(file5)
#
#     # 计算智能体、TDMA 和 SA 的吞吐量
#     throughput_agent = np.cumsum(agent_reward) / np.arange(1, len(agent_reward) + 1)
#     throughput_tdma = np.cumsum(tdma_reward) / np.arange(1, len(tdma_reward) + 1)
#     throughput_sa = np.cumsum(sa_reward) / np.arange(1, len(sa_reward) + 1)
#
#     # 绘制吞吐量曲线
#     plt.plot(throughput_agent + throughput_tdma + throughput_sa, color='#236B8E', lw=1.2,
#              label='Total')
#     plt.plot(sa_reward_smoothed, color='orange', lw=1.2, label='slottedALOHA')
#     plt.plot(tdma_reward_smoothed, color='green', lw=1.2, label='TDMA')
#
#     # 每隔1000个时隙添加一个原点标记
#     indices = np.arange(0, max_iter, 1000)
#     plt.scatter(indices, throughput_agent[indices] + throughput_tdma[indices] + throughput_sa[indices],
#                 color='#236B8E', s=30, marker='o')  # Total 曲线添加原点标记
#
#     plt.scatter(indices, sa_reward_smoothed[indices], color='orange', s=30, marker='o')  # slottedALOHA 曲线添加原点标记
#     plt.scatter(indices, tdma_reward_smoothed[indices], color='green', s=30, marker='o')  # TDMA 曲线添加原点标记
#
#     # 设置图例、坐标轴和标题
#     plt.legend()
#     plt.xlim((0, max_iter))
#     plt.ylim((0, 1))
#     plt.xlabel('Time Slot')
#     plt.ylabel('Throughput')
#     plt.title('Network Throughput Comparison')
#
#
# if __name__ == '__main__':
#     env1 = ENVIRONMENT()
#     TONT = calculate_TONT(env1)
#     print("TONT =", TONT)
#
#     plt.figure()
#     my_plot('rewards/agent_len5e4_M40_111.txt',
#             'rewards/TDMA_len5e4_M40_111.txt',
#             'rewards/SA_len5e4_M40_111.txt',
#             'rewards/only_SA_111.txt',
#             'rewards/only_TDMA_111.txt')
#
#     # 显示图形
#     plt.show()
import numpy as np
import matplotlib.pyplot as plt
from env_111 import ENVIRONMENT
env1=ENVIRONMENT()
# 按照公式计算最优的吞吐量！!!!!!

p = 1 / 3 # TDMA节点个数/总的节点个数
#对于 z<0：𝑇𝑜𝑝𝑡=𝑝⋅(1−𝑞1)+(1−𝑝)⋅𝑞1⋅(1−𝑞1)
#z≥0：𝑇𝑜𝑝𝑡=(1−𝑞1)


TONT = 0
n=1
print(env1.z)
if env1.z < 0:
    TONT = p * (1-env1.SAaction) + (1 - p) * env1.SAaction * (1-env1.SAaction)
else:
    TONT = 1 - env1.SAaction

# 输出最优网络吞吐量
print("TONT=",TONT)

def my_plot(file1, file2, file3, file4, file5):
    # 设置最大迭代次数
    max_iter = 10000

    # 从文本文件中加载智能体、TDMA 和 SA 的奖励数据
    agent_reward = np.loadtxt(file1)
    tdma_reward = np.loadtxt(file2)
    sa_reward = np.loadtxt(file3)

    # 加载已经平滑处理过的 SA 和 TDMA 的奖励数据
    sa_reward_smoothed = np.loadtxt(file4)
    tdma_reward_smoothed = np.loadtxt(file5)

    # 计算智能体、TDMA 和 SA 的吞吐量
    throughput_agent = np.cumsum(agent_reward) / np.arange(1, len(agent_reward) + 1)
    throughput_tdma = np.cumsum(tdma_reward) / np.arange(1, len(tdma_reward) + 1)
    throughput_sa = np.cumsum(sa_reward) / np.arange(1, len(sa_reward) + 1)

    # 绘制吞吐量曲线
    total_line, = plt.plot(np.arange(1, max_iter + 1), throughput_agent + throughput_tdma + throughput_sa,
                           color='#236B8E', lw=1.4,label='Total Throughput')
    sa_line, = plt.plot(np.arange(1, max_iter + 1), sa_reward_smoothed, color='orange', lw=1, label='Slotted ALOHA')
    tdma_line, = plt.plot(np.arange(1, max_iter + 1), tdma_reward_smoothed, color='green', lw=1, label='TDMA')

    # 每隔1000个时隙添加一个原点标记
    # indices = np.arange(0, max_iter, 1000)
    # plt.scatter(indices, throughput_agent[indices] + throughput_tdma[indices] + throughput_sa[indices],
    #             color='#236B8E', s=30)  # Total 曲线添加原点标记
    #
    # plt.scatter(indices, sa_reward_smoothed[indices], color='orange', s=30)  # Slotted ALOHA 曲线添加原点标记
    # plt.scatter(indices, tdma_reward_smoothed[indices], color='green', s=30)  # TDMA 曲线添加原点标记

    # 设置图例、坐标轴和标题
    plt.legend(handles=[total_line, sa_line, tdma_line])
    plt.xlim((0, max_iter))
    plt.ylim((0, 1))
    plt.xlabel('Time Slot')
    plt.ylabel('Throughput')
    plt.title('Network Throughput Comparison')

    # 显示图例
    plt.legend()




plt.figure()
my_plot('rewards/agent_len5e4_M40_111.txt',
            'rewards/TDMA_len5e4_M40_111.txt',
            'rewards/SA_len5e4_M40_111.txt',
            'rewards/only_SA_111.txt',
            'rewards/only_TDMA_111.txt')

    # 显示图形
plt.show()
